package cn.edu.mju.property.service.impl;


import cn.edu.mju.property.dao.ComplaintDao;
import cn.edu.mju.property.dao.impl.ComplaintDaoImpl;
import cn.edu.mju.property.entity.Complaint;
import cn.edu.mju.property.entity.PageBean;
import cn.edu.mju.property.service.ComplaintService;
import com.alibaba.fastjson.JSONArray;

import java.util.List;

public class ComplaintServiceImpl implements ComplaintService {

    private ComplaintDao dao = new ComplaintDaoImpl();

    @Override
    public PageBean<Complaint> findUserByPage(String _currentPage, String _rows) {

        int currentPage = Integer.parseInt(_currentPage) ;
        int rows = Integer.parseInt(_rows) ;

        PageBean<Complaint> pb = new PageBean<Complaint>();
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //总记录数
        int totalCount = dao.findTotalCount();
        pb.setTotalCount(totalCount);

        //开始值
        int start = (currentPage - 1) * rows;
        List<Complaint> list = dao.findByPage(start,rows);
        pb.setList(list);

        int totalPage = totalCount % rows == 0 ? (totalCount/rows) : (totalCount/rows) + 1;
        pb.setTotalPage(totalPage);

        return pb;

    }

    @Override
    public boolean delCompaint(JSONArray ids) {
        for (Object id: ids) {
            dao.delSelected(id.toString());
        }
        return true;
    }

    @Override
    public boolean updateComplaint(Complaint complaint) {
        return dao.upComplaint(complaint);
    }

    @Override
    public boolean upComplaintStatus(String id, String status) {
        return dao.upComplaintStatus(id,status);
    }

    @Override
    public Complaint findComplaint(String id) {
        return dao.findComplaint(id);
    }


}
